<template>
    <div>
        <div>
            <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
                <el-breadcrumb-item :to="{ path: '/sys-admin/index' }">
                    <i class="el-icon-s-promotion"></i> 首页
                </el-breadcrumb-item>
                <el-breadcrumb-item :to="{ path: '/sys-admin/temp/album/add-new' }">
                    添加相册
                </el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
        </div>
        <div style="margin:3% 15% 0 0 ; width: 70%">

            <el-form :model="AlbumAddNew" :rules="rules" ref="AlbumAddNew" label-width="100px" class="demo-ruleForm">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="AlbumAddNew.name"></el-input>
                </el-form-item>
                <el-form-item label="简介" prop="description">
                    <el-input v-model="AlbumAddNew.description"></el-input>
                </el-form-item>
                <el-form-item label="排序序号" prop="sort">
                    <el-input v-model="AlbumAddNew.sort"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('AlbumAddNew')">立即创建</el-button>
                    <el-button @click="resetForm('AlbumAddNew')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                AlbumAddNew: {
                    name: '',
                    description:'',
                    sort:'',
                },
                rules: {
                    name: [
                        {required: true, message: '请输入名称', trigger: 'blur'},
                        {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                    ],
                    description: [
                        {required: true, message: '请输入简介', trigger: 'blur'},
                        {min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur'}
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        console.log(this.AlbumAddNew)
                        this.axios.post("http://localhost:9080/albums/add-new",this.qs.stringify(this.AlbumAddNew))
                            .then((response) =>{
                                if (response.data.state=20000){
                                    this.$message.success("添加成功")
                                }else{
                                    this.$message.error(response.data.message)
                                }
                            })
                    } else {
                        console.log('请检查信息是否有误');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>